﻿// See https://aka.ms/new-console-template for more information
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using UseZXing;
using ZXing;
using ZXing.QrCode;

Console.WriteLine("Hello, World!");

var imagePath = "C:\\Img\\2.jpg";
// 加载图片
Bitmap bitmap = new Bitmap(imagePath);

//bitmap = MakeGrayscale3(bitmap); 
var barcodeBitmap = (Bitmap)Image.FromFile(imagePath);


// 创建泛型条形码阅读器
IBarcodeReader barcodeReader = new BarcodeReader();
//var result = barcodeReader.Decode(barcodeBitmap);

// 设置多个条形码
//barcodeReader.Options = new ZXing.Common.DecodingOptions
//{
//    TryHarder = true,
//    //MultiFormat = true
//};
try
{
    // 识别条形码
    ZXing.Result[] results = barcodeReader.DecodeMultiple(GetRgbArray(bitmap), bitmap.Width, bitmap.Height, RGBLuminanceSource.BitmapFormat.RGB24);

    // 处理识别结果
    if (results != null && results.Length > 0)
    {
        Console.WriteLine("识别到的条形码数量: " + results.Length);

        foreach (var result in results)
        {
            Console.WriteLine($"条形码内容: {result.Text}, 类型: {result.BarcodeFormat}");
        }
    }
    else
    {
        Console.WriteLine("未识别到条形码");
    }

}
catch (Exception e)
{
    Console.WriteLine(e.Message);
    throw;
}



/// <summary>
/// 处理图片灰度
/// </summary>
static Bitmap MakeGrayscale3(Bitmap original)
{
    //create a blank bitmap the same size as original
    Bitmap newBitmap = new Bitmap(original.Width, original.Height);
    //get a graphics object from the new image
    Graphics g = Graphics.FromImage(newBitmap);
    //create the grayscale ColorMatrix
    System.Drawing.Imaging.ColorMatrix colorMatrix = new ColorMatrix(
        new float[][]
        {
            new float[] {.3f, .3f, .3f, 0, 0},
            new float[] {.59f, .59f, .59f, 0, 0},
            new float[] {.11f, .11f, .11f, 0, 0},
            new float[] {0, 0, 0, 1, 0},
            new float[] {0, 0, 0, 0, 1}
      });
    //创建一些图像属性
    ImageAttributes attributes = new ImageAttributes();
    //设置颜色矩阵属性
    attributes.SetColorMatrix(colorMatrix);
    //在新图像上绘制原始图像
    //使用灰度颜色矩阵
    g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
       0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
    //dispose the Graphics object
    g.Dispose();
    return newBitmap;
}


static byte[] BitmapToByteArray(Bitmap bitmap)
{
    using (MemoryStream stream = new MemoryStream())
    {
        // 将图像保存到 MemoryStream 中
        bitmap.Save(stream, ImageFormat.Jpeg); // 根据实际图像格式进行选择

        // 将 MemoryStream 中的数据转为 byte[]
        return stream.ToArray();
    }
}

///获取img的tRgb
static byte[] GetRgbArray(Bitmap bitmap)
{
    // 获取图像数据
    BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

    try
    {
        int stride = bmpData.Stride;
        int bytesPerPixel = 3; // 24 bits per pixel
        int dataSize = stride * bitmap.Height;

        byte[] rgbValues = new byte[dataSize];
        IntPtr scan0 = bmpData.Scan0;

        // 将图像数据复制到数组中
        Marshal.Copy(scan0, rgbValues, 0, dataSize);

        // 将数据从 BGR 转换为 RGB
        for (int i = 0; i < dataSize; i += bytesPerPixel)
        {
            byte temp = rgbValues[i];
            rgbValues[i] = rgbValues[i + 2];
            rgbValues[i + 2] = temp;
        }

        return rgbValues;
    }
    finally
    {
        // 解锁图像数据
        bitmap.UnlockBits(bmpData);
    }
}